package com.ifcar.app.activity;


import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import com.ifcar.app.download.AppUpdateService;
import com.ifcar.app.util.Tools;
import com.ifcar.app.activity.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DialogActivity extends Activity {
	
	private TextView dialog_content;
	private ProgressBar dialog_pb;
	private Button btn_positive;
	private Button btn_negative;
	private String updateContent;
	private Timer timer;
	private Map updatemap = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.dialog_activity);
		
		new Thread(){
			
			public void run(){
				
			    updatemap = Tools.checkAppUpdate(DialogActivity.this);
				UIHandler.sendEmptyMessage(1);
				
			}
			
		}.start();
		
		
		btn_negative = (Button) findViewById(R.id.btn_negative);
		btn_positive = (Button) findViewById(R.id.btn_positive);
		dialog_content = (TextView) findViewById(R.id.dialog_content);
		dialog_pb = (ProgressBar) findViewById(R.id.dialog_pb);
		btn_positive.setVisibility(View.GONE);
		btn_negative.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				DialogActivity.this.finish();
			}
			
		});
		
		btn_positive.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent it = new Intent(DialogActivity.this,AppUpdateService.class);
				DialogActivity.this.startService(it);
				DialogActivity.this.finish();
				
			}
		});
		timer = new Timer();
		timer.schedule(task, 30000);
	}
	
	private TimerTask task = new TimerTask() {

		@Override
		public void run() {
			UIHandler.sendEmptyMessage(2);
		}
		
	};
	
	private Handler UIHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			switch(msg.what) {
            case 1:
                dialog_pb.setVisibility(View.GONE);

                if (null!=updatemap) {
                    dialog_content.setText(R.string.update_find);
                    btn_positive.setText(R.string.btn_download);
                    btn_positive.setVisibility(View.VISIBLE);
                    btn_negative.setVisibility(View.VISIBLE);
                } else {
                    btn_negative.setVisibility(View.VISIBLE);
                    btn_positive.setVisibility(View.GONE);
                    btn_negative.setText(R.string.btn_negative);
                    dialog_content.setText(R.string.update_not_find);
                }
                timer.cancel();
                break;
			case 2:
				dialog_pb.setVisibility(View.GONE);
				dialog_content.setText(R.string.update_failed);
				btn_negative.setVisibility(View.VISIBLE);
				btn_positive.setVisibility(View.GONE);
				btn_negative.setText(R.string.btn_negative);
				break;
			}
				
		}
	};
}
